<?php
namespace Admin\Controller;
use Org\Util\ArrayToolkit;

class ConfigController extends AdminController {
    public function _initialize()
    {
        parent::_initialize();
        $this->model = D('Config');
    }

    public function index() {
        $this->IS_POST(function () {
                $map = M('Config')->create() ?: array();
                $map = array_filter($map);
                if (I('keyword')) {
                    $kw = '%' . str_replace(' ', '%', I('keyword')) . '%';
                    $map['name|title|id|remark'] = array('like', $kw);
                }
                $data = D('Config')->where($map)->order('sort desc')->findPage();
                $this->ajaxReturn($data);
        });
        $this->_web('title', '配置管理');
        $this->_web('panel_title', '配置管理');
        $listData = array(
            'url' => U(''),
            'toolbar' => '#barDemo',
            'toolbarTpl' => 'toolbar',
            'page' => true,
        );
        $listData['cols'][] = array('type' => 'checkbox', 'fixed' => 'left');
        $listData['cols'][] = array('field' => 'id', 'title' => 'ID', 'sort' => true);
        $listData['cols'][] = array('field' => 'name', 'title' => '名称', 'width' => '20%');
        $listData['cols'][] = array('field' => 'title', 'title' => '标题', 'width' => '25%');
        $listData['cols'][] = array('field' => 'group_text', 'title' => '分组', 'width'=>'86');
        $listData['cols'][] = array('field' => 'type_text', 'title' => '类型','width'=>'100');
        $listData['cols'][] = array('field' => 'sort', 'title' => '排序','width'=>'60');
        $listData['cols'][] = array('fixed' => 'right','width'=>'220', 'title' => '操作', 'toolbar'=>'#rowBar');
        $groups = D('Category')->getChilds('config');
        $groups = ArrayToolkit::KeyAndValue($groups, 'id', 'title');
        $this->assign('groups', $groups);
        $this->displayList($listData);
    }
    public function add()
    {
        $id = I('id', '', 'intval');
        $model = D('Config');
        if (IS_POST) {
            $data = $model->create();
            if (!$data) {
                $this->error($model->getError());
            } else if ($model->in2replace($data, array('id'=>$id)) !== false) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
            return true;
        }
        $cate = D('Category')->getChilds('config');
        $option = array();
        foreach ($cate as $row) {
            $option[$row['id']] = $row['title'];
        }
        if ($id) {
            $info = $model->find($id);
            $this->assign('info', $info);
        }
        $this->assign('groups', $option);
        $this->display();
    }
    public function edit_field ($id){
        $info = $this->model->find($id);
        if(!$info) {
            $this->error('配置不存在');
        }
        $info['extra_arr'] = D('Config', 'Api')->parse_config_attr($info['extra']);
        $this->assign('info', $info);
        $this->display();
    }
    public function view($id) {
        $info = $this->model->find($id);
        if(!$info) {
            $this->error('配置不存在');
        }
        $this->assign('info', $info);
        $this->display();
    }
    public function _beging_del($ids, &$url) {
        $url = '';
    }
}
